package  {{.basepackage}}.controller;

import org.springframework.web.bind.annotation.PostMapping;
import cn.pinming.pmsuite.WebContext;
import cn.pinming.pmsuite.api.ApiResult;
import cn.pinming.pmsuite.common.search.PagedSearchRequest;
import cn.pinming.pmsuite.persistence.PagedList;
import cn.pinming.pmsuite.utils.DateUtils;
import cn.pinming.pmsuite.utils.TypeUtils;
import cn.pinming.pmsuite.utils.StringUtils;
import cn.pinming.pmsuite.web.security.UserAccount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import {{.basepackage}}.entity.{{.PascalName}};
import {{.basepackage}}.service.{{.PascalName}}Service;

/**
 * @version 1.0.0
 * @Project : {{.SiteName}}
 * @Package Name : {{.basepackage}}.controller.{{.PascalName}}Controller
 * @Company 杭州品茗信息技术有限公司
 * @Author 林芳崽 on {{ .CreateTime }}.
 * @Description: {{.table.TableComment}}控制器
 */
@Controller("{{.domainname}}_{{.Lowername}}controller")
@RequestMapping(value="/{{.domainname}}/{{.Lowername}}")
public class {{.PascalName}}Controller  {
    private   final Logger logger = LoggerFactory.getLogger({{.PascalName}}Controller.class);
	@Autowired
	private {{.PascalName}}Service {{.CamelName}}Service;

    /**
    * 列表
    *
    * @return
    */
    @RequestMapping("/index")
    public String index() {
        return "/{{.domainname}}/{{.Lowername}}/list";
    }

    /**
     * 获取单条记录
     *
     * @return
     */
    @RequestMapping("/get")
    public String get(Long id, boolean edit, Model model) {
        //应用基本信息
        {{.PascalName}} viewModel;
        if (null != id) {
            viewModel = {{.CamelName}}Service.getByKey(id);
        } else {
            viewModel = new {{.PascalName}}();
        }
        model.addAttribute("data", viewModel);
        if (null == id || edit) {
            return  "/{{.domainname}}/{{.Lowername}}/edit";
        }
        return  "/{{.domainname}}/{{.Lowername}}/detail";
    }



    /**
     *  分页数据
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public PagedList<{{.PascalName}}> list(PagedSearchRequest<{{.PascalName}}> request) {
        return {{.CamelName}}Service.getPaged(request);
    }



    /**
     * 保存更新
     *
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public ApiResult save(@Valid {{.PascalName}} data, HttpServletRequest request) {
        UserAccount sessionUser = WebContext.getUserAccount( );
        ApiResult result;
        try {
            if (StringUtils.isNullOrEmpty(data.getId())) {
                data.setCreateTime(DateUtils.now());
                result = {{.CamelName}}Service.create(data);
            } else {
                result = {{.CamelName}}Service.update(data);
            }
        } catch (Exception e) {
            logger.debug("保存信息异常：{}", e.getMessage());
            result=ApiResult.create();
            result.error("保存信息失败");
        }
        return result;
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ResponseBody
    public ApiResult delete(String ids) {
        ApiResult result =ApiResult.create();
        try {
            List<String> idList = TypeUtils.parseToStrList(ids);
            if (idList.size() == 0) {
                return result.error("请选中一条记录删除");
            }
            for (String id : idList) {
                result = {{.CamelName}}Service.deleteByKey(id);
            }
        } catch (Exception e) {
            logger.debug("删除异常：{}", e.getMessage());

            result=ApiResult.create();
            result.error("删除失败");
        }
        return result;
    }
}
